package com.demo.image;

import javax.activation.MimetypesFileTypeMap;
import java.io.File;

/**
 * @author wangning
 * @date 2023/5/5 10:26
 */
public class ImageCheck {
    private MimetypesFileTypeMap fileTypeMap;

    public ImageCheck() {
        fileTypeMap = new MimetypesFileTypeMap();
        /*
         * 不添加以下类型会造成误判，详见
         * https://stackoverflow.com/questions/4855627/java-mimetypesfiletypemap-always-returning-application-octet-stream-on-android-e
         */
        fileTypeMap.addMimeTypes("image png tif jpg jpeg bmp gif");

    }

    public boolean isImage(File file) {
        String contentType = fileTypeMap.getContentType(file);
        String type = contentType.split("/")[0];
        return type.equals("image");
    }


}
